package com.yongoe.ecy.thirdNode.controller;

import com.yongoe.ecy.secondNode.entity.SecondNode;
import com.yongoe.ecy.thirdNode.service.ThirdNodeService;
import com.yongoe.ecy.config.aop.WebLog;
import com.yongoe.ecy.thirdNode.controller.vo.excel.ThirdNodeExcel;
import com.yongoe.ecy.thirdNode.controller.vo.req.ThirdNodeReq;
import com.yongoe.ecy.thirdNode.controller.vo.res.ThirdNodeRes;
import com.yongoe.ecy.thirdNode.convert.ThirdNodeConvert;
import com.yongoe.ecy.thirdNode.entity.ThirdNode;
import com.yongoe.ecy.utils.ExcelUtils;
import com.yongoe.ecy.utils.PageUtils;
import com.yongoe.ecy.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 三级节点
 *
 * @author monad
 * @since 2024/03/10
 */
@Tag(name = "三级节点")
@RestController
@RequestMapping("/thirdNode/thirdnode")
public class ThirdNodeController {
    @Resource
    private ThirdNodeService thirdNodeService;
    @Resource
    private ThirdNodeConvert thirdNodeConvert;

    //@WebLog
    @Operation(summary = "查询分页数据")
    @PostMapping("/page")
    public R page(@RequestBody ThirdNodeReq req) {
        ThirdNode entity = thirdNodeConvert.req2Entity(req);
        Page<ThirdNode> page = thirdNodeService.getThirdNodeByPage(Page.of(req.getCurrent(), req.getSize()), entity);
        return R.success().put(new PageUtils(page));
    }

    @WebLog
    @Operation(summary = "添加三级节点")
    @PostMapping("/add")
    public R add(@RequestBody ThirdNode thirdNode) {
        Long nodeID = thirdNodeService.addThirdNode(thirdNode);
        thirdNode.setNodeId(nodeID);
        thirdNodeService.save(thirdNode);
        return R.success("添加成功");
    }

    @WebLog
    @Operation(summary = "修改三级节点")
    @PostMapping("/update")
    public R update(@RequestBody ThirdNode thirdNode) {
        thirdNodeService.updateThirdNode(thirdNode);
        thirdNode.setSecondNodeName(null);
        thirdNodeService.updateById(thirdNode);
        return R.success("修改成功");
    }

    @WebLog
    @Operation(summary = "删除三级节点")
    @PostMapping("/delete/{ids}")
    public R delete(@PathVariable Long[] ids) {
        thirdNodeService.deleteThirdNodes(ids);
        thirdNodeService.deleteByIds(ids);
        return R.success("删除成功");
    }

    // @WebLog
    // @Operation(summary = "导入数据")
    // @PostMapping("/upload")
    // public R upload(MultipartFile file) {
    //     List<ThirdNodeExcel> excelList = ExcelUtils.upload(file, ThirdNodeExcel.class);
    //     List<ThirdNode> list = thirdNodeConvert.excel2EntityList(excelList);
    //     thirdNodeService.saveBatch(list);
    //     return R.success("导入成功");
    // }

    // @WebLog
    // @Operation(summary = "导出数据")
    // @GetMapping("/export")
    // public void export(ThirdNodeReq req, HttpServletResponse response) {
    //     ThirdNode entity = thirdNodeConvert.req2Entity(req);
    //     Page<ThirdNode> page = thirdNodeService.getThirdNodeByPage(Page.of(-1, -1), entity);
    //     List<ThirdNodeExcel> list = thirdNodeConvert.entity2ExcelList(page.getRecords());
    //     ExcelUtils.export(response, list, ThirdNodeExcel.class);
    // }
}